/*
 * Copyright 2007 Guy Van den Broeck
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.outerj.daisy.diff.html.ancestor.tagtostring;

import java.util.MissingResourceException;
import java.util.ResourceBundle;

import org.outerj.daisy.diff.html.ancestor.ChangeText;
import org.outerj.daisy.diff.html.ancestor.TagChangeSematic;
import org.outerj.daisy.diff.html.dom.TagNode;
import org.outerj.daisy.diff.html.modification.HtmlLayoutAddition;
import org.outerj.daisy.diff.html.modification.HtmlLayoutChange;
import org.outerj.daisy.diff.html.modification.HtmlLayoutRemoval;
import org.xml.sax.Attributes;

public class TagToString {

    protected TagNode node;

    private ChangeType sem;

	private ResourceBundle bundle;

    protected TagToString(TagNode node, TagChangeSematic sem,
            ResourceBundle bundle) {
        this.node = node;
        this.setChangeType(sem);
        this.bundle = bundle;
        
    }

    public String getDescription() {
    	
        return getString("diff-" + node.getQName());
        
    }

    public HtmlLayoutChange getRemovedDescription(ChangeText txt) {
    	HtmlLayoutChange htmlLayoutChange = new HtmlLayoutRemoval();
    	addChangeDescription(txt, htmlLayoutChange);
    	return htmlLayoutChange;
    }
    public void addChangeDescription(ChangeText txt, HtmlLayoutChange htmlLayoutChange) {
    	htmlLayoutChange.setEndingTag(node.getEndTag());
    	htmlLayoutChange.setOpeningTag(node.getOpeningTag());

        sem.addChangeDescription(txt, htmlLayoutChange, this);
        addAttributes(txt, node.getAttributes());
        txt.addText(".");
    }

    public HtmlLayoutChange getAddedDescription(ChangeText txt) {
    	HtmlLayoutChange htmlLayoutChange = new HtmlLayoutAddition();
    	addChangeDescription(txt, htmlLayoutChange);
    	return htmlLayoutChange;
    }

    protected void addAttributes(ChangeText txt, Attributes attributes) {
        if (attributes.getLength() < 1)
            return;

        txt.addText(" " + getWith().toLowerCase() + " "
                + translateArgument(attributes.getQName(0)) + " "
                + attributes.getValue(0));
        for (int i = 1; i < attributes.getLength() - 1; i++) {
            txt.addText(", " + translateArgument(attributes.getQName(i)) + " "
                    + attributes.getValue(i));
        }
        if (attributes.getLength() > 1) {
            txt.addText(" "
                    + getAnd().toLowerCase()
                    + " "
                    + translateArgument(attributes.getQName(attributes
                            .getLength() - 1)) + " "
                    + attributes.getValue(attributes.getLength() - 1));
        }
    }

    private String getAnd() {
        return getString("diff-and");
    }

    private String getWith() {
        return getString("diff-with");
    }

    protected String translateArgument(String name) {
        if (name.equalsIgnoreCase("src"))
            return getSource().toLowerCase();
        if (name.equalsIgnoreCase("width"))
            return getWidth().toLowerCase();
        if (name.equalsIgnoreCase("height"))
            return getHeight().toLowerCase();
        return name;
    }

    private String getHeight() {
        return getString("diff-height");
    }

    private String getWidth() {
        return getString("diff-width");
    }

    protected String getSource() {
        return getString("diff-source");
    }

    public String getArticle() {
        return getString("diff-" + node.getQName() + "-article");
    }

    public String getString(String key) {
        try {
            return bundle.getString(key);
        } catch (MissingResourceException e) {
            return '!' + key + '!';
        }
    }

	public void setChangeType(TagChangeSematic sem) {
		switch (sem) {
		case STYLE:
			this.sem = new ChangeStyle();
			break;
		case MOVED:
			this.sem = new ChangeMoved();
			break;
		default:
			this.sem = null;
			break;
		}
	}

	public TagChangeSematic getChangeType() {
		return sem.getChangeType();
	}
}
